﻿using Theater.DataModels;
using Theater.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Theater.Controllers
{
    public class HomeController : Controller
    {
        List<Movie> Movies = new List<Movie>();
        public ActionResult Index()
        {
            //Read
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Movies = db.Movies.ToList();
            }
            return View(Movies);
        }
        [HttpGet]
        public ActionResult AddMovie()
        {

            return View();
        }
        [HttpPost]
        public ActionResult AddMovie(string title, string image)
        {
            Movie movie = new Movie();
            //Create
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Movies.Add(new Movie()
                {
                    Title = title,
                    Image = image
                });
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            //Delete
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var target = db.Movies.Find(id);
                db.Movies.Remove(target);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Movie m = db.Movies.Find(id);
                return View(m);
            }
        }
        public ActionResult Edit(Movie movie)
        {
            //Update
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Movie m = db.Movies.Where(x => x.Id == movie.Id).FirstOrDefault();
                m.Title = movie.Title;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

    }
}